#!/bin/bash
########################################
#  This file is part of the open-eBackup project.
# Copyright (c) 2024 Huawei Technologies Co.,Ltd.
#
# open-eBackup is licensed under MPL v2.
# You can use this software according to the terms and conditions of the MPL v2.
# You may obtain a copy of MPL v2 at:
#
#          https://www.mozilla.org/en-US/MPL/2.0
#
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
# See the MPL v2 for more details.
########################################

set -eux
set -o pipefail

tmp_cpus=$(grep -w processor /proc/cpuinfo|wc -l)

# install zstd
cp zstd-1.5.0-2.oe2203sp1.src.rpm /opt/buildtools
pushd /opt/buildtools
rpm2cpio zstd-1.5.0-2.oe2203sp1.src.rpm | cpio -di
tar -zxf zstd-1.5.0.tar.gz
cd zstd-1.5.0
make -j ${tmp_cpus}
make install
cp /opt/buildtools/zstd-1.5.0/lib/libzstd.so.1.5.0 /usr/lib64/
ln -sf  /usr/lib64/libzstd.so.1.5.0 /usr/lib64/libzstd.so.1
popd


if [ $OS_TYPE == "CentOS-6" ];then
    ls gmp-6.1.0.tar.bz2  mpc-1.0.3.tar.gz mpfr-3.1.4.tar.bz2 gcc-7.3.0.tar.gz
    # clear
    install_dir=$1
    [ -d "${install_dir}" ] && rm -rf ${install_dir}
    mkdir -p ${install_dir}
    # install gmp
    gmp_install_dir="${install_dir}/gmp-6.1.0"
    [ -d "${gmp_install_dir}" ] && rm -rf ${gmp_install_dir}
    mkdir -p ${gmp_install_dir}
    tar jxf gmp-6.1.0.tar.bz2
    cd gmp-6.1.0
    ./configure --prefix=${gmp_install_dir}
    make -j ${tmp_cpus}
    make install
    # install mpfr
    mpfr_install_dir="${install_dir}/mpfr-3.1.4"
    [ -d "${mpfr_install_dir}" ] && rm -rf ${mpfr_install_dir}
    mkdir -p ${mpfr_install_dir}
    cd ..
    tar jxf mpfr-3.1.4.tar.bz2
    cd mpfr-3.1.4
    ./configure --prefix=${mpfr_install_dir} --with-gmp=${gmp_install_dir}
    make -j ${tmp_cpus}
    make install
    # install mpc
    mpc_install_dir="${install_dir}/mpc-1.0.3"
    [ -d "${mpc_install_dir}" ] && rm -rf ${mpc_install_dir}
    mkdir -p ${mpc_install_dir}
    cd ..
    tar xf mpc-1.0.3.tar.gz
    cd mpc-1.0.3
    ./configure --prefix=${mpc_install_dir} --with-gmp=${gmp_install_dir} --with-mpfr=${mpfr_install_dir}
    make -j ${tmp_cpus}
    make install    
else

    # check install files
    ls gmp-6.2.1.tar.xz  mpc-1.2.1.tar.gz mpfr-4.1.0.tar.gz gcc-7.3.0.tar.gz
    # clear
    install_dir=$1
    [ -d "${install_dir}" ] && rm -rf ${install_dir}
    mkdir -p ${install_dir}
    tmp_cpus=$(grep -w processor /proc/cpuinfo|wc -l)
    
    # install gmp
    gmp_install_dir="${install_dir}/gmp-6.2.1"
    [ -d "${gmp_install_dir}" ] && rm -rf ${gmp_install_dir}
    mkdir -p ${gmp_install_dir}
    tar xf gmp-6.2.1.tar.xz
    cd gmp-6.2.1
    ./configure --prefix=${gmp_install_dir}
    make -j ${tmp_cpus}
    make install
    
    # install mpfr
    mpfr_install_dir="${install_dir}/mpfr-4.1.0"
    [ -d "${mpfr_install_dir}" ] && rm -rf ${mpfr_install_dir}
    mkdir -p ${mpfr_install_dir}
    cd ..
    tar xf mpfr-4.1.0.tar.gz
    cd mpfr-4.1.0
    ./configure --prefix=${mpfr_install_dir} --with-gmp=${gmp_install_dir}
    make -j ${tmp_cpus}
    make install
    
    # install mpc
    mpc_install_dir="${install_dir}/mpc-1.2.1"
    [ -d "${mpc_install_dir}" ] && rm -rf ${mpc_install_dir}
    mkdir -p ${mpc_install_dir}
    cd ..
    tar xf mpc-1.2.1.tar.gz
    cd mpc-1.2.1
    ./configure --prefix=${mpc_install_dir} --with-gmp=${gmp_install_dir} --with-mpfr=${mpfr_install_dir}
    make -j ${tmp_cpus}
    make install

fi

# install patch
cd ..
tar xf gcc-7.3.0.tar.gz

# install gcc
cd gcc-7.3.0
echo "${mpc_install_dir}/lib" >> /etc/ld.so.conf.d/libc.conf
echo "${gmp_install_dir}/lib" >> /etc/ld.so.conf.d/libc.conf
echo "${mpfr_install_dir}/lib" >> /etc/ld.so.conf.d/libc.conf
ldconfig
# libsanitizer/sanitizer_common
sed -i '/sys\/ustat.h/d' libsanitizer/sanitizer_common/sanitizer_platform_limits_posix.cc
sed -i '/struct ustat/d' libsanitizer/sanitizer_common/sanitizer_platform_limits_posix.cc

./configure --prefix=${install_dir} --enable-threads=posix  --disable-checking --disable-multilib --enable-languages=c,c++ --with-mpc=${mpc_install_dir} --with-gmp=${gmp_install_dir} --with-mpfr=${mpfr_install_dir}

#make install gcc
make -j ${tmp_cpus}
make install

# Environment Variable
sed -i '/GCC_HOME/d' /etc/profile
echo "export GCC_HOME=${install_dir}" >> /etc/profile
echo "export PATH=\$GCC_HOME/bin:\$PATH" >> /etc/profile
echo 'export LD_LIBRARY_PATH=/usr/lib64:$LD_LIBRARY_PATH ' >> /etc/profile

echo "${install_dir}/lib64" >> /etc/ld.so.conf.d/libc.conf
ldconfig
# OS method
update-alternatives --install /usr/local/bin/gcc gcc ${install_dir}/bin/gcc 100
update-alternatives --install /usr/local/bin/g++ g++ ${install_dir}/bin/g++ 100

#add link
mv /usr/bin/gcc /usr/bin/gcc_bak
ln -s /opt/buildtools/gcc-7.3.0/bin/gcc /usr/bin/gcc
mv /usr/bin/c++ /usr/bin/c++_bak
ln -s /opt/buildtools/gcc-7.3.0/bin/c++ /usr/bin/c++
mv /usr/bin/g++ /usr/bin/g++_bak
ln -s /opt/buildtools/gcc-7.3.0/bin/g++ /usr/bin/g++

ln -sf ${install_dir}/bin/gcc  ${install_dir}/bin/cc
ln -sf ${install_dir}/bin/gcc /usr/local/bin/gcc
ln -sf ${install_dir}/bin/cc /usr/local/bin/cc
ln -sf ${install_dir}/bin/g++ /usr/local/bin/g++
ln -sf ${install_dir}/bin/c++ /usr/local/bin/c++
ln -sf ${install_dir}/bin/gcc-ar /usr/local/bin/gcc-ar
ln -sf ${install_dir}/bin/gfortran /usr/local/bin/gfortran
#update libstdc++ library

rm -rf /lib64/libstdc++*
cp ${install_dir}/lib64/libstdc++.so.6.0.24 /usr/lib64/
chmod 755 /usr/lib64/libstdc++.so.6.0.24
ln -sf /usr/lib64/libstdc++.so.6.0.24 /usr/lib64/libstdc++.so.6
